import apis from '@/apis'
import type { SiteRequest } from '@/apis/modules/site'
import type { CollectManageRequest } from '@/apis/modules/collectManage'
import { ref, reactive } from 'vue'
import { message } from 'ant-design-vue'

type ObjectType = Record<string, string | number | boolean>

export type FilterValuesType = number[]

export type StrType = string
export type NumType = number | null | undefined

export type SelectItemType = { label: string; value: number | undefined }
export type SelectItemType1 = { label: string; value: number }
export interface ResourceLibrarySiteClassificationType {
  label: StrType
  value: NumType
  isBind: NumType
}
// 采集状态
const COLLECT_START_STATE: SelectItemType[] = [
  { label: '全部状态', value: 0 },
  { label: '启用', value: 1 },
  { label: '禁用', value: 2 },
  { label: '采集中', value: 3 }
]
const COLLECT_START_STATE_2: SelectItemType[] = [
  { label: '启用', value: 1 },
  { label: '禁用', value: 2 }
]
const COLLECT_START_STATE_3: SelectItemType[] = [
  { label: '全部状态', value: 0 },
  { label: '成功', value: 1 },
  { label: '失败', value: 2 }
]
const COLLECT_START_STATE_4: SelectItemType[] = [
  { label: '全部状态', value: 0 },
  { label: '已采集', value: 1 },
  { label: '未采集', value: 2 }
]

// const COLLECT_STATE: SelectItemType1[] = [
//   { label: '全部状态', value: 0 },
//   { label: '已采集', value: 1 },
//   { label: '采集中', value: 2 },
//   { label: '未采集', value: 3 },
//   { label: '启用', value: 4 },
//   { label: '禁用', value: 5 },
//   { label: '成功', value: 6 },
//   { label: '失败', value: 7 }
// ]

// 资源类型
const RESOURCE_TYPE: SelectItemType[] = [
  { label: '全部资源类型', value: 0 },
  { label: '种源资源', value: 1 }
  // { label: '外部资源', value: 2 }
]

// 内容类型
const CONTENT_TYPE: SelectItemType[] = [
  { label: '全部内容类型', value: 0 },
  { label: '影片', value: 1 },
  { label: '图集', value: 2 },
  { label: '文章', value: 3 }
]

// 内容分发类型
const CONTENT_DISTRIBUTE_TYPE: SelectItemType[] = [
  { label: '立即上线', value: 1 },
  { label: '下线状态', value: 2 }
  // { label: '定时上线', value: 3 }
]

// 链接类型
const LINK_TYPE: SelectItemType[] = [
  { label: '磁力链接资源', value: 1 },
  { label: 'MP4链接资源', value: 2 },
  { label: 'M3U8链接资源', value: 3 }
]

// 接口类型
const API_TYPE: SelectItemType[] = [
  { label: 'xml', value: 1 },
  { label: 'json', value: 2 }
]

// 数据处理
const DATA_PROCESSING: SelectItemType[] = [
  { label: '新增+更新', value: 1 },
  { label: '仅采集新增', value: 2 },
  { label: '仅更新', value: 3 }
]

// 采集范围
const COLLECT_RANGE: SelectItemType[] = [
  { label: '采集全部影片', value: 1 },
  { label: '仅采集已绑定分类影片', value: 2 }
]

// 采集时间类型
const COLLECT_TIME_TYPE: SelectItemType[] = [
  { label: '采当天', value: 1 },
  { label: '采本周', value: 2 },
  { label: '采所有', value: 0 }
]

// 价格设定
const PRICE_SETTING: SelectItemType[] = [
  { label: '原价', value: 1 },
  { label: '固定', value: 2 },
  { label: '随机', value: 3 }
]
// 采集规则
const COLLECT_DATE_RULE: Record<number, any> = {
  1: '每天',
  2: '每周',
  3: '每月'
}

// 采集站点 (API获取)
const STATION = ref<SelectItemType[]>([])
const getStation = (params?: SiteRequest['site.list']) => {
  console.log('params: ', params)
  const { list } = apis.site
  list(params).then((res: any) => {
    console.log('站点：', res)
    const stations = res.sites.map((m: ObjectType) => ({ label: m.site_name, value: m.site_id }))
    STATION.value = [{ label: '全部站点', value: 0 }, ...stations]
  })
}

/**
 * 站点分类 (API获取)
 * 1.获取查询 (影片|图集|文章) 分类接口
 * 2.设置 SITE_CLASSIFICATION 值
 */
export interface SiteClassificationItemType {
  id: string
  name: string
}
export interface SiteClassificationType extends SiteClassificationItemType {
  subcateg: SiteClassificationItemType[]
}

const SITE_CLASSIFICATION = ref<SiteClassificationType[]>([])

// 1.获取查询 (影片|图集|文章) 分类接口
type SiteClassificationApiType = number // 1：影片  2：图集  3：文章
const getSiteClassificationApi = (type: SiteClassificationApiType) => {
  const { classify_list, atlas_classify_list, article_classify_list } = apis.site
  switch (type) {
    case 1:
      return classify_list
    case 2:
      return atlas_classify_list
    case 3:
      return article_classify_list
  }
}

// 根据 站点id 获取 影片/图集/文章 分类数据
type SiteClassificationParamsType = { site_id: string }
const getSiteClassification = (type: SiteClassificationApiType, params: SiteClassificationParamsType) => {
  const requestApi = getSiteClassificationApi(type) as Function
  requestApi(params).then((res: any) => {
    // if (!res.categ.length) message.warning('未采集到站点分类信息，请重新选择其他站点')
    console.log('站点分类：', res)
    SITE_CLASSIFICATION.value = res.categ
  })
}

// 站点分类-资源库 (API获取)
const RESOURCE_LIBRARY_SITE_CLASSIFICATION: ResourceLibrarySiteClassificationType[] = [
  {
    label: '电影/喜剧',
    value: 1,
    isBind: 1
  },
  {
    label: '电影/动作',
    value: 2,
    isBind: 0
  }
]

// 采集资源 (API获取)
let COLLECT_RESOURCES = ref<SelectItemType[]>([])
const getCollectResources = (status = 1) => {
  const { getCollectManagerList } = apis.collectManage
  getCollectManagerList({ status }).then((res: any) => {
    console.log('res1: ', res.list)
    COLLECT_RESOURCES.value = res?.list ?? []
    console.log('COLLECT_RESOURCES.value: ', COLLECT_RESOURCES.value)
  })
}

/**
 * @description: select 数据过滤
 * @param {any} source 数据源
 * @param {any} values 要过滤的值
 * @return {SelectItemType[]} 返回 value !== 0 的项
 */
const filterSelectSource = (source: SelectItemType[], values: FilterValuesType = []): SelectItemType[] => {
  return source.filter((f: SelectItemType) => {
    if (![...values].includes(f.value as number)) return f
  })
}

/**
 * @description: select 数据格式化
 * @param {any} source 数据源
 * @param {any} value 值
 * @return {string} 返回对应 value 的 label
 */
const selectValueFormatter = (source: SelectItemType[], value: number): string => {
  return source.find((f: SelectItemType) => f.value === value)?.label ?? '-'
}

export {
  COLLECT_START_STATE,
  COLLECT_START_STATE_2,
  COLLECT_START_STATE_3,
  COLLECT_START_STATE_4,
  // COLLECT_STATE,
  RESOURCE_TYPE,
  CONTENT_TYPE,
  CONTENT_DISTRIBUTE_TYPE,
  LINK_TYPE,
  API_TYPE,
  DATA_PROCESSING,
  COLLECT_RANGE,
  COLLECT_TIME_TYPE,
  PRICE_SETTING,
  COLLECT_DATE_RULE,
  STATION,
  SITE_CLASSIFICATION,
  RESOURCE_LIBRARY_SITE_CLASSIFICATION,
  COLLECT_RESOURCES,
  getStation,
  getSiteClassification,
  filterSelectSource,
  selectValueFormatter,
  getCollectResources
}
